﻿#include "businesschat.h"
#include <UserManager/usermanager.h>
#include <AppConfig/appconfig.h>

#include "businessnetwork.h"
#include "businessgroup.h"
#include "businessfile.h"
#include <SyncManager/syncmanager.h>

BusinessChat::BusinessChat() {}

void BusinessChat::chatEvent(QJsonObject &in, QJsonObject &out)
{
    bool chatToUser = in["sendToUser"].toBool();
    // if not chat to User
    if (chatToUser) {
        chatToFriend(in, out);
    } else {
        chatToGroup(in, out);
    }
}

void BusinessChat::chatToFriend(QJsonObject &in, QJsonObject &out)
{ 
    QString account = in["TargetAccount"].toString();

    UserManager& manager = UserManager::getInstance();
    User& targetUser = manager.getTargetUser(in["TargetAccount"].toString().toStdString());

    auto id = targetUser["UserID"];
    AppConfig& config = AppConfig::getInstance();
    uint64_t selfID = std::stoi(config.runtimeValue("UserID"));

    nlohmann::json message;
    message["Message"] = in["Message"].toString().toStdString();
    message["MessageType"] = in["MessageType"].toInt();

    std::string messageStr = message.dump();

    BusinessNetWork::getInstance().sendMessageToServer(1, 1, selfID, id, 0,
                                                       (unsigned char*)messageStr.data(), messageStr.size());
}

void BusinessChat::chatToGroup(QJsonObject &in, QJsonObject &out)
{
    QString QgroupName = in["TargetAccount"].toString();
    std::string groupName = QgroupName.toStdString();

    nlohmann::json groupInfo = BusinessGroup::getInstance().getTargetGroup(groupName);
    uint64_t groupID = groupInfo["GroupID"];

    nlohmann::json message;
    message["Message"] = in["Message"].toString().toStdString();
    message["MessageType"] = in["MessageType"].toInt();

    std::string messageStr = message.dump();

    AppConfig& config = AppConfig::getInstance();
    uint64_t selfID = std::stoi(config.runtimeValue("UserID"));

    BusinessNetWork::getInstance().sendMessageToServer(1, 2, selfID, groupID, 0,
                                                       (unsigned char*)messageStr.data(), messageStr.size());
}

void BusinessChat::sendPicture(QJsonObject &in, QJsonObject &out) {
    BusinessNetWork& net = BusinessNetWork::getInstance();

    QJsonObject Fin, Fout;
    Fin["FileSize"] = in["FileSize"];
    Fin["FilePath"] = in["FilePath"];
    Fin["FileType"] = 2;
    Fin["FileName"] = std::to_string(net.requestUniqueID()).c_str();

    in["Message"] = Fin["FileName"];
    in["MessageType"] = 2;

    BusinessFile::getInstance().uploadFile(Fin, Fout);
    if (Fout["result"].toBool()) {
        // 图片传输成功
        chatEvent(in, out);
    }
}

void BusinessChat::requestOfflineMessage(QJsonObject &in, QJsonObject &out)
{
    // request offline message

    int64_t targetUserID = in["TargetUserID"].toInteger();

    UserManager& manager = UserManager::getInstance();
    AppConfig& config = AppConfig::getInstance();
    User& self = manager.getTargetUser(config.runtimeValue("Account"));
    auto selfID = self["UserID"];

    BusinessNetWork::getInstance().sendMessageToServer(1, 3, selfID, targetUserID,0, nullptr, 0);
}

void BusinessChat::getPicture(QJsonObject& in, QJsonObject& out)
{
    std::string fileName = in["FileName"].toString().toStdString();
    std::string filePath = "./picture/" + fileName;
    if (std::filesystem::exists(filePath)) {
        return ;
    }
    // 向服务器发出请求

    BusinessFile::getInstance().downloadFile(in, out);
    uint64_t syncPoint = std::stoull(out["SyncPoint"].toString().toStdString());
    SyncManager& syncManager = SyncManager::getInstance();
    syncManager.waitForSyncPoint(syncPoint);
}
